/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Marks;

import Student.StudentBean;
import Subject.SubjectBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument;

/**
 *
 * @author Mukesh
 */
public class GetMarksReportDetail extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int tmin = 0, tmax = 0;
        try {
            int aid = 0, subid = 0, rollno = 0;
            if (request.getParameter("id").length() != 0) {
                aid = Integer.parseInt(request.getParameter("id"));
            }
            if (request.getParameter("rollno").length() != 0) {
                rollno = Integer.parseInt(request.getParameter("rollno"));
            }

            String sem = request.getParameter("sem");
            String stuname = request.getParameter("stuname");
            String stream = request.getParameter("stream");

            HashMap hm = Marks.Query.getMarksReport(aid, subid);

            Vector mblist = (Vector) hm.get("mblist");
            Vector sblist = (Vector) hm.get("sblist");

            Vector<ExamType.ExamTypeBean> list1 = ExamType.Query.viewAll();
            out.println("<a href='javascript:void(0)' onclick='quickClose()'>");
            out.println("<span id='quickclose'><img src='/RIT/img/close.png'></span></a>");
            out.println("<div id='records'>");
            out.println("<div class='rowood' style='border-bottom:1px solid #13A;'>");
            out.println("<div><b style='color:black;'>Student Name : " + stuname + "&nbsp; </b></div>");
            out.println("<div><b style='color:black;'>Roll No:&nbsp; " + rollno + "</b></div>");
            out.println("</div>");
            out.println("<div class='roweven'>");
            out.println("<div><b>Subject&nbsp; </b></div>");
            out.println("<div><b>Min Marks&nbsp; </b></div>");
            out.println("<div><b>Max Marks&nbsp; </b></div>");
            for (int k = 0; k < list1.size(); k++) {
                out.println("<div><b>" + list1.get(k).getExamTypeName() + "&nbsp; </b></div>");
            }
            out.println("</div>");
//          //  Vector list = Subject.Query.getSubject(stream, sem);
//            Iterator it = list.iterator();
//            HashMap<Integer,Integer> total = new HashMap<Integer, Integer>();
//            while (it.hasNext()) {
//                int duplicate = 0;
//                int k=0;
//                out.println("<div>");
//                SubjectBean sb = (SubjectBean) it.next();
//                out.println("<div><b>" + sb.getSubjectName() + "&nbsp; </b></div>");
//                for (int i = 0; i < mblist.size(); i++) {
//                    
//                    MarksBean mb = (MarksBean) mblist.get(i);
//                    SubjectBean sb1 = (SubjectBean) sblist.get(i);
//                    
//                    if (sb.getSubjectId() == mb.getSubid()) {
//                        
//                        if (duplicate == 0) {
//                            out.println("<div>" + sb1.getMinMarks() + "&nbsp; </div>");
//                            out.println("<div>" + sb1.getTotalMarks() + "&nbsp; </div>");
//                            out.println("<div>" + mb.getMarks() + "&nbsp; </div>");
//                            tmin = tmin + sb1.getMinMarks();
//                            tmax = tmax + sb1.getTotalMarks();
//                            total.put(mb.getExamtypeid(), k + mb.getMarks());
//                            k=total.get(mb.getExamtypeid());
//                        } else {
//                            out.println("<div>" + mb.getMarks() + "&nbsp; </div>");
//                            total.put(mb.getExamtypeid(), k + mb.getMarks());
//                            k=total.get(mb.getExamtypeid());
//                        }
//                        k++;
//                        duplicate = 1;
//                    }
//                }
//                out.println("</div>");
//            }
            Vector subjectlist = Subject.Query.getSubject(stream, sem);
            Iterator it = subjectlist.iterator();
            HashMap<Integer, Integer> total = new HashMap<Integer, Integer>();
            while (it.hasNext()) {
                SubjectBean sb = (SubjectBean) it.next();
                out.println("<div>");
                out.println("<div><b>" + sb.getSubjectName() + "&nbsp; </b></div>");
                try {
                    Connection con = DBConnection.Connection.getConnection();

                    PreparedStatement pre = con.prepareStatement("select total_marks,min_marks,weightage,theory_practical from subject where subject_id=?");
                    pre.setInt(1, sb.getSubjectId());
                    ResultSet rs = pre.executeQuery();
                    while (rs.next()) {
                        out.println("<div>" + rs.getInt("MIN_MARKS") + "&nbsp; </div>");
                        out.println("<div>" + rs.getInt("TOTAL_MARKS") + "&nbsp; </div>");
                        tmin = tmin + rs.getInt("MIN_MARKS");
                        tmax = tmax + rs.getInt("TOTAL_MARKS");
                        Statement st = con.createStatement();
                        ResultSet rs1 = st.executeQuery("select * from exam_type order by exam_type_id");
                        while (rs1.next()) {
                            Statement st2 = con.createStatement();
                            ResultSet rs2 = st2.executeQuery("select marks from marks where admission_id=" + aid + " and subject_id=" + sb.getSubjectId() + " and exam_type_id=" + rs1.getInt("exam_type_id"));
                            if (rs2.next()) {
                                out.println("<div>" + rs2.getInt("marks") + "&nbsp; </div>");
                                if (total.get(rs1.getInt("exam_type_id")) == null) {
                                        total.put(rs1.getInt("exam_type_id"), (rs2.getInt("marks")));
                                } else {
                                    total.put(rs1.getInt("exam_type_id"), (rs2.getInt("marks") + total.get(rs1.getInt("exam_type_id"))));
                                }
                            } else {
                                out.println("<div>-&nbsp; </div>");
                            }
                        }
                    }
                    out.println("</div>");

                } catch (Exception e) {
                    out.println(e);
                }

            }

            out.println("<div class='rowood'>");
            out.println("<div><b>Total&nbsp; </b></div>");
            out.println("<div><b>" + tmin + "&nbsp; </b></div>");
            out.println("<div><b>" + tmax + "&nbsp; </b></div>");
            for (int k = 0; k < list1.size(); k++) {
                out.println("<div><b >" + total.get(list1.get(k).getExamTypeId()) + "&nbsp; </b></div>");
            }
            out.println("</div>");

            out.println("<div>");
            out.println("</div>");

            out.println("</div>");
            out.println("<div id='fade' class='black_overlay'></div>");

        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
